package com.auemall.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auemall.constant.ApplicationConstant;
import com.auemall.dao.BuyerGradesPOMapper;
import com.auemall.dao.TBuyerPOMapper;
import com.auemall.dao.TTicketPOMapper;
import com.auemall.po.BuyerGradesPO;
import com.auemall.po.TBuyerPO;
import com.auemall.po.TTicketPO;
import com.auemall.po.TTicketPOExample;
import com.auemall.service.TicketService;
import com.auemall.utils.EUDataGridResult;
import com.auemall.utils.StringUtils;
import com.auemall.vo.BaseRespVO;
import com.auemall.vo.TicketReqVO;
import com.auemall.vo.TicketVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class TicketServiceImpl implements TicketService {
	
	private Logger logger = Logger.getLogger(TicketServiceImpl.class);
	
	@Autowired
	private TTicketPOMapper ticketPOMapper;		//开票信息表
	
	@Autowired
	private TBuyerPOMapper buyerPOMapper;		//会员表
	
	@Autowired
	private BuyerGradesPOMapper buyerGradeMapper;		//会员等级表

	
	/**
	 * 
	 * 查询开票信息
	 * 
	 */
	@Override
	public EUDataGridResult selectTicketList(TicketReqVO req, int page, int rows) throws Exception {
		EUDataGridResult respVO = new EUDataGridResult();
		//非空验证
		if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
		
		
		
		//查询开票信息列表
		TTicketPOExample example = new TTicketPOExample();
		TTicketPOExample.Criteria criteria = example.createCriteria();
		//审核状态
		if(StringUtils.isNotBlank(req.getStatus())){
			criteria.andAuditEqualTo(Integer.parseInt(req.getStatus()));
		}
		
		//会员名
		if(StringUtils.isNotBlank(req.getBuyerName())){
			//
			List<TTicketPO> ticketByName = ticketPOMapper.selectByBuyerName(req.getBuyerName());
			for (TTicketPO tTicketPO : ticketByName) {
				criteria.andBuyeridEqualTo(tTicketPO.getBuyerid());
			}
			if(ticketByName == null || ticketByName.size() == 0){
				criteria.andBuyeridEqualTo(-1);
			}
		}
		//开始时间
		if(StringUtils.isNotBlank(req.getAddTime_begin())){
			criteria.andCreatedateGreaterThanOrEqualTo(ApplicationConstant.dateTimeFormat.parse(req.getAddTime_begin()));
		}
		//结束时间
		if(StringUtils.isNotBlank(req.getAddTime_end())){
			criteria.andCreatedateLessThanOrEqualTo(ApplicationConstant.dateTimeFormat.parse(req.getAddTime_end()));
		}
		//设置分页
		PageHelper.startPage(page, rows);
		List<TTicketPO> list = ticketPOMapper.selectByExample(example);
		
		//页面结果集
		List<TicketVO> tickets = new ArrayList<TicketVO>();
		for (TTicketPO ticket : list) {
			TicketVO ticketVO = new TicketVO();
			ticketVO.setId(ticket.getId());
			ticketVO.setBuyerid(ticket.getBuyerid());
			ticketVO.setAudit(ticket.getAudit());
			//查询会员
			TBuyerPO buyer = buyerPOMapper.selectByPrimaryKey(ticket.getBuyerid());
			//查询会员级别
			BuyerGradesPO buyerGrade = buyerGradeMapper.selectByPrimaryKey(buyer.getLevel());
			ticketVO.setBuyerName(buyer.getUsername());
			ticketVO.setLevel(buyerGrade.getName());
			tickets.add(ticketVO);
		}
		respVO.setRows(tickets);
		//设置总条数
		PageInfo<TTicketPO> pageInfo = new PageInfo<>(list);
		respVO.setTotal(pageInfo.getTotal());
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("查询开票信息成功");
        return respVO;
	}

	@Override
	public BaseRespVO addTicketInfo(TicketVO req) {
		
		return null;
	}

	/**
	 * 审核开票资料
	 */
	@Override
	public BaseRespVO updateTicketInfo(TicketVO req,Integer flag) throws Exception{
		BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        //查询开票信息
        TTicketPO ticketPO = ticketPOMapper.selectByPrimaryKey(req.getId());
        if(ticketPO == null || ticketPO.getId() == null){
        	respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("此开票信息不存在！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        //修改开票信息
        ticketPO.setAudit(flag);
        
        //保存到数据库
        int result = ticketPOMapper.updateByPrimaryKeySelective(ticketPO);
        respVO.setResult(String.valueOf(result));
		
		return respVO;
	}

	@Override
	public BaseRespVO deleteTicketInfo(String[] ids) {
		BaseRespVO respVO = new BaseRespVO();
		if(ids == null || ids.length <= 0){
			respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
		}
		int result = 0;
		//循环删除
		for (String idStr : ids) {
			result = ticketPOMapper.deleteByPrimaryKey(Integer.parseInt(idStr));
		}
		respVO.setResult(String.valueOf(result));
		return respVO;
	}

	/**
	 * 根据ID查询发票信息
	 */
	@Override
	public TicketVO selectTicketById(Integer id) throws Exception{
		//非空验证
		if(null == id){
            return null;
        }
		TTicketPO ticketPO = ticketPOMapper.selectByPrimaryKey(id);
		TicketVO ticketVO = new TicketVO();
		
		BeanUtils.copyProperties(ticketPO, ticketVO);
		
		
		return ticketVO;
	}

}
